package com.red.wine.view;

import com.red.wine.R;
import com.red.wine.util.BitmapUtil;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
/**
 * 整体加载图片暂未使用
 * @author yhl
 * TODO
 * 2013-10-11上午9:53:42
 * {@link description}
 */
public class MyTurnplateView extends View {
	
	/**
	 * 画笔：点、线
	 */
	private Paint mPaint = new Paint();
	/**
	 * 图标列表
	 */
	private Bitmap[] icons = new Bitmap[10];
	/**
	 * point列表
	 */
	private Point[] points;
	/**
	 * 数目
	 */
	private static final int PONIT_NUM = 6;
	
	/**
	 * 圆心坐标
	 */
	private int mPointX=0, mPointY=0;
	/**
	 * 半径
	 */
	private int mRadius = 0;
	/**
	 * 每两个点间隔的角度
	 */
	private int mDegreeDelta;
	/**
	 * 选中的图标标识 999：未选中任何图标
	 */
	private int chooseBtn=999;
	private Matrix mMatrix = new Matrix();  
	private Context context;
	public MyTurnplateView(Context context, int px, int py, int radius) {
		super(context);		
		this.context = context;
		loadIcons();
		mPointX = px;
		mPointY = py;
		mRadius = radius;
		
		initPoints();
		computeCoordinates();
	}
	
	
	/**
	 * 
	 * 方法名：loadIcons 
	 * 功能：获取所有图片
	 */
	public void loadIcons(){
		Resources r = getResources();	
		loadBitmaps(0, r.getDrawable(R.drawable.taste_press), 90);
		loadBitmaps(1, r.getDrawable(R.drawable.appreciate_press), 0);
		loadBitmaps(2, r.getDrawable(R.drawable.recommend_press), 0);
		loadBitmaps(3, r.getDrawable(R.drawable.purchase_press), 0);
		loadBitmaps(4, r.getDrawable(R.drawable.community_press),0);
		loadBitmaps(5, r.getDrawable(R.drawable.my_center_press), 0);
	}
	/**
	 * 方法名：loadBitmaps 
	 * 功能：装载图片
	 */
	public void loadBitmaps(int key,Drawable d, int rotate){
		Bitmap bitmap = Bitmap.createBitmap(BitmapUtil.drawable2Bitmap(d).getWidth(),BitmapUtil.drawable2Bitmap(d).getHeight(),Bitmap.Config.ARGB_8888);
		bitmap = BitmapUtil.rotateBitmap(context, rotate, bitmap);
		Canvas canvas = new Canvas(bitmap);
		d.setBounds(0, 0, BitmapUtil.drawable2Bitmap(d).getWidth(),BitmapUtil.drawable2Bitmap(d).getHeight());
		d.draw(canvas);
		icons[key]=bitmap;
	}
	
	/**
	 * 方法名：initPoints 
	 * 功能：初始化每个点
	 */
	 
	private void initPoints() {
		points = new Point[PONIT_NUM];
		Point point;
		int angle = -90;
		mDegreeDelta =360/PONIT_NUM;
		
		for(int index=0; index<PONIT_NUM; index++) {
			point = new Point();
			point.angle = angle;
			angle += mDegreeDelta;
			point.bitmap = icons[index];
			point.flag=index;
			points[index] = point;
		}
	}
	
	/**
	 * 
	 * 方法名：computeCoordinates 
	 * 功能：计算每个点的坐标
	 */
	private void computeCoordinates() {
		Point point;
		for(int index=0; index<PONIT_NUM; index++) {
			point = points[index];
			point.x = mPointX+ (float)(mRadius * Math.cos(point.angle*Math.PI/180));
			point.y = mPointY+ (float)(mRadius * Math.sin(point.angle*Math.PI/180));	
			point.x_c = mPointX+(point.x-mPointX)/2;
			point.y_c = mPointY+(point.y-mPointY)/2;
		}
	}
	
	
	
	
	@Override
	public void onDraw(Canvas canvas) {
		for(int index=0; index<PONIT_NUM; index++) {
			drawInCenter(canvas, points[index].bitmap, points[index].x, points[index].y,points[index].flag);
		}
	}
	
	/**
	 * 
	 * 方法名：drawInCenter 
	 * 功能：把点放到图片中心处
	 */
	void drawInCenter(Canvas canvas, Bitmap bitmap, float left, float top,int flag) {
		canvas.drawPoint(left, top, mPaint);
		if(chooseBtn==flag){
			mMatrix.setScale(bitmap.getWidth(), bitmap.getHeight());   
			mMatrix.postTranslate(left, top);  
			canvas.drawBitmap(bitmap, mMatrix, null); 
		}else{
			canvas.drawBitmap(bitmap, left-bitmap.getWidth()/2, top-bitmap.getHeight()/2, null);
		}
		
		
	}	
	
	class Point {
		
		/**
		 * 位置标识
		 */
		int flag;
		/**
		 * 图片
		 */
		Bitmap bitmap;
		
		/**
		 * 角度
		 */
		int angle;
		
		/**
		 * x坐标
		 */
		float x;
		
		/**
		 * y坐标
		 */
		float y;
		
		/**
		 * 点与圆心的中心x坐标
		 */
		float x_c;
		/**
		 * 点与圆心的中心y坐标
		 */
		float y_c;
		
	}
}
